package com.myfoon.action.backing.system;

import com.myfoon.model.cacheable.UserCache;
import com.myfoon.model.ejb.UserEJBLocal;
import com.myfoon.model.entity.User;
import com.myfoon.model.entity.UserPi;
import com.myfoon.model.entryprocessor.FriendSelector;
import com.myfoon.view.application.MyConstants;
import com.myfoon.view.context.helper.EJBReceiver;

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;

import java.util.Iterator;
import java.util.List;

import java.util.Map;

import javax.ejb.EJBException;

import javax.persistence.EntityExistsException;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;

import org.hibernate.exception.ConstraintViolationException;

public class UserBean {
    public UserBean() {
    }
    
    public User persist(User user) throws EntityExistsException, ConstraintViolationException {
        EJBReceiver receiver = new EJBReceiver();
        
        UserEJBLocal local =
            (UserEJBLocal)receiver.receiveEJB(MyConstants.jndiName + "UserEJB/local");
        
        try {
            user = local.persist(user);
        }
        catch(EJBException e) {
            if (e.getCausedByException() instanceof EntityExistsException) {                
                throw new EntityExistsException();
            }
            e.printStackTrace();
        }
        
        return user;    
    }


    public User merge(User user) {
        return null;
    }

    public void remove(User user) {
    }

    public User findByEmailAndPassword(String email, String passwd) throws NoResultException, EntityNotFoundException {
        EJBReceiver receiver = new EJBReceiver();
        UserEJBLocal local =
            (UserEJBLocal)receiver.receiveEJB(MyConstants.jndiName + "UserEJB/local");
        
        User user = null;    
        try {
            user = (User)local.findByEmailAndPassword(email, passwd);
        }
        catch(EJBException e) {
            if (e.getCausedByException() instanceof NoResultException)
                throw new NoResultException();
            if (e.getCausedByException() instanceof EntityNotFoundException)
                throw new EntityNotFoundException();
        }        
        return user;
    }
    
    public Map<Long, List> findFriendINPs(User user, int first, int max) {
        EJBReceiver receiver = new EJBReceiver();
        UserEJBLocal local =
            (UserEJBLocal)receiver.receiveEJB(MyConstants.jndiName + "UserEJB/local");
        
        Object[] obj = local.findRandomFriends(user, 6);
        System.out.println(obj[0]);
        List<UserCache> friends = (List<UserCache>)obj[1];
        for (UserCache c : friends) {
            System.out.println(c.getId() + ":" + c.getFullname() + ":" + c.getCountryId());            
        }
        
        return local.findFriendINPs(user, first, 50);
    }
    
    public Object[] findPagedFriends(User user, int start) {
        EJBReceiver receiver = new EJBReceiver();
        UserEJBLocal local =
            (UserEJBLocal)receiver.receiveEJB(MyConstants.jndiName + "UserEJB/local");
        return local.findPagedFriends(user, start, 50);
    }
    
    public Object[] findRandomFriends(User user) {
        EJBReceiver receiver = new EJBReceiver();
        UserEJBLocal local =
            (UserEJBLocal)receiver.receiveEJB(MyConstants.jndiName + "UserEJB/local");
        return local.findRandomFriends(user, 16);
    }
}
